package com.enation.app.javashop.core.payment.plugin.weixin;

import com.enation.app.javashop.core.payment.model.vo.PayBill;
import com.enation.app.javashop.core.payment.service.AbstractPaymentPlugin;
import com.enation.app.javashop.framework.context.ThreadContextHolder;
import com.enation.app.javashop.framework.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author fk
 * @version v2.0
 * @Description: 微信相关config
 * @date 2018/4/1810:14
 * @since v7.0.0
 */
public class WeixinPuginConfig extends AbstractPaymentPlugin {

    public static final String OPENID_SESSION_KEY = "weixin_openid";
    public static final String UNIONID_SESSION_KEY = "weixin_unionid";
    public static final String QR_URL_PREFIX = "weixin://wxpay/bizpayurl[?]pr=";
    public static final String CACHE_KEY_PREFIX = "pay_";

    public final String siteName = "javashop商城订单";


    @Override
    protected String getPluginId() {

        return "weixinPayPlugin";
    }

    /**
     * double转成分
     * @param money
     * @return
     */
    protected String toFen(Double money) {
        String value = BigDecimal.valueOf(money).multiply(new BigDecimal(100)).toString();

        NumberFormat numberFormat = new DecimalFormat("##");

        return numberFormat.format(new BigDecimal(value));
    }

    /**
     * 设置参数
     */
    public Map<String,String> setConfig(PayBill bill) {
        Map<String, String> config = this.getConfig(bill.getClientType());
        WeixinPayConfig.appId = config.get("appid");
        WeixinPayConfig.mchId = config.get("mchid");
        WeixinPayConfig.key = config.get("key");

        Map<String,String> params = new TreeMap();
        params.put("appid", WeixinPayConfig.appId);
        params.put("mch_id", WeixinPayConfig.mchId);
        params.put("nonce_str", StringUtil.getRandStr(10));
        params.put("body", siteName);
        params.put("out_trade_no", bill.getOutTradeNo());
        // 应付转为分
        Double money = bill.getOrderPrice();
        if(money!=null) {
            params.put("total_fee", toFen(money));
        }
        params.put("notify_url",this.getCallBackUrl(bill.getTradeType(),bill.getClientType()));
        return params;
    }

    /**
     * 获取ip
     * @return
     */
    protected String getIpAddress(){
        HttpServletRequest request = ThreadContextHolder.getHttpRequest();
        String ip = request.getHeader("x-forwarded-for");
        if(StringUtil.isEmpty(ip)){
            return request.getRemoteAddr();
        }
        if(ip.contains(",")){
            ip = ip.split(",")[0];
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Cdn-Src-Ip");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
